package com.hunt.service.impl;

import com.hunt.dao.MainDataMapper;
import com.hunt.model.dto.PageInfo;
import com.hunt.model.entity.MainDateNew;
import com.hunt.service.MainDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class MainDataServiceImpl implements MainDataService {

    private static final Logger log = LoggerFactory.getLogger(MainDataServiceImpl.class);

    @Autowired
    private MainDataMapper mainDataMapper;

    @Override
    public PageInfo selectPage(int page, int rows, int fieldNo, int sortNo) {
        int counts = mainDataMapper.countbyfundhold();
        page = (page-1) * rows;
        String order = 0 == sortNo ? "asc" : "desc";
        String fieldName = getFieldNameByNo(fieldNo);
        List<MainDateNew> mainDataList = mainDataMapper.selectAllbyfundhold(page, rows, fieldName, order);
        PageInfo pageInfo = new PageInfo(counts, mainDataList);
        return pageInfo;
    }

    private String getFieldNameByNo(int fieldNo) {
        String fieldName = "ownFundNumber";
        if(0 == fieldNo) {
            fieldName = "ownFundNumber";
        } else if(1 == fieldNo) {
            fieldName = "holdingNumber";
        } else if(2 == fieldNo) {
            fieldName = "holdingStockValue";
        } else if(3 == fieldNo) {
            fieldName = "holdingChangeValue";
        } else if(4 == fieldNo) {
            fieldName = "holdingChangeRate";
        }
        return fieldName;
    }

}
